<template>
  <div class="conversion" @click.stop="aotuTab=false">
    <jc-button
      :btn-arr="['reload', 'save', 'remove', 'pass', 'sub', 'option', 'list']"
      :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'copy', 'freeze', 'com1', 'com2', 'chart', 'toExcel']"
      :list="'/conversionList'"
      @del="delCustom"
      @refresh="conversion"
      @save="save"
      @audit="Retrial(1)"
      @noAudit="Retrial(2)"
      @submit="save(1)"
      @noSubmit="materSub(2)"
      @forbidden="forbidden(1)"
      @startUsing="forbidden(2)"
    />
    <el-tabs>
      <el-tab-pane :label="$t('market.info')">
        <el-form :model="operVal" size="mini" label-width="100px">
          <el-form-item :label="$t('market.state')">
            <el-tag :type="operVal.stateColor">{{ operVal.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model.trim="operVal.fnumber" :disabled="isDisabled" @input="getAtuo(operVal.fnumber)">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="isMaterielDialog=true"
              />
            </el-input>
            <div v-if="aotuTab" class="tabPosit">
              <number-code :fnumber="fnumber1" @material="materielSelectRow" />
            </div>
          </el-form-item>
          <el-form-item :label="$t('market.fmodel')" prop="fmodel">
            <el-input v-model="operVal.fmodel" disabled />
          </el-form-item>
          <el-form-item :label="$t('shop.fname2')" prop="fname">
            <el-input v-model="operVal.fname" disabled />
          </el-form-item>
          <el-form-item :label="$t('shop.fspecificaTion')" prop="fspecificaTion">
            <el-input v-model="operVal.fspecificaTion" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.fdescripTion')" prop="fdescripTion">
            <el-input v-model="operVal.fdescripTion" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.relation')">
            <el-input-number v-model="operVal.fconvertdenominator" class="num" :disabled="isDisabled" />
            * <el-input v-model="operVal.fcurrentunitname" class="num" :disabled="isDisabled">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="unitVisabled=true"
              />
            </el-input> =
            <el-input-number v-model="operVal.fconvertnumerator" :disabled="isDisabled" class="num" />
            * <el-input v-model="operVal.fdestunitname" class="num" disabled />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <!--    物料弹窗-->
    <material v-if="isMaterielDialog" @material="materielSelectRow" />
    <baseUnit v-if="unitVisabled" :msg="false" @unit="selUnit" />
  </div>
</template>

<script>
import {
  deleteUnit,
  queryUnitDetail, unitAudit, unitDisable, unitSubmit,
  unitUnAudit, unitUnDisable, unitUnSubmit, updateUnit
} from '@/api/basicManagement/customerBom'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'
export default {
  components: {
    jcButton,
    baseUnit: () => import('@/views/basic/createMateriel2/components/baseUnit'),
    numberCode: () => import('@/components/NumberCode'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material')
  },
  data() {
    return {
      isDisabled: false,
      isMaterielDialog: false, // 物料弹窗
      aotuTab: false,
      fnumber1: '',
      unitVisabled: false,
      operVal: {}
    }
  },
  created() {
    if (this.$route.params.id) {
      this.conversion()
    }
  },
  methods: {
    // 详情
    conversion() {
      const fid = this.$route.params.id
      queryUnitDetail({ fid }).then(res => {
        if (res.code === 0) {
          this.operVal = toMxBmina(res.data)
          this.isDisabled = this.operVal.fdocumentstatus === this.$t('market.state1') || this.operVal.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 修改
    save(ev) {
      this.operVal = notoMxBmina(this.operVal)
      updateUnit({ ...this.operVal }).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.materSub(1) : this.conversion()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 输入物料编码
    getAtuo(fnumber) {
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber1 = fnumber
      }, 1000)
    },
    // 物料
    materielSelectRow(item) {
      if (item.fmaterialId) {
        const items = this.operVal
        items.fmaterialId = item.fmaterialId
        items.fnumber = item.fnumber
        items.fname = item.fname
        items.fmodel = item.fmodel
        items.fspecificaTion = item.fspecificaTion
        items.fdescripTion = item.fdescripTion
        items.fcurrentunitname = item.funit
        items.fdestunitname = item.funit
        items.fcurrentunitid = item.funitId
        items.fdestunitid = item.funitId
      }
      this.isMaterielDialog = false
    },
    // 单位
    selUnit(ev) {
      if (ev.funitid) {
        this.operVal.fcurrentunitname = ev.fname
        this.operVal.fcurrentunitid = ev.funitid
      }
      this.unitVisabled = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.operVal.fid + ''] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
        this.conversion()
      })
    },
    // 提交
    async materSub(ev) {
      const api = ev === 1 ? unitSubmit : unitUnSubmit
      this.toExamineFun(api)
    },
    // 审核
    async Retrial(ev) {
      const api = ev === 1 ? unitAudit : unitUnAudit
      this.toExamineFun(api)
    },
    // 禁用
    async forbidden(ev) {
      const api = ev === 1 ? unitDisable : unitUnDisable
      this.toExamineFun(api)
    },
    // 删除
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const DATA = { Numbers: [this.operVal.fid + ''] }
        deleteUnit(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/conversionList' })
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.conversion {
  @include listBom;
  background: #FFF;
  padding: 10px;
  .tabPosit {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: 3.5vh;
    left: 0vw;
    background-color: rgba(255, 255, 255, 1);
    z-index: 99999!important;
  }
  .num {
    width: 80px!important;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    //max-width: 300px;
    .el-form-item {
      width: 500px;
    }
    &::v-deep .el-input-number .el-input__inner{
      padding: 0 5px;
      height: 30px;
      width: 75px;
    }
    &::v-deep .el-input-number .el-input{
      width: 100%;
    }
    &::v-deep .el-input-number--mini{
      width: 100%;
    }
    &::v-deep .el-input-number__increase{
      display: none;
    }
    &::v-deep .el-input-number__decrease{
      display: none;
    }
    &::v-deep .el-table__footer-wrapper tbody td{
      background: #fdf6ec!important;
    }
  }
}
</style>

